package com.gec.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gec.model.entity.AliPayBean;
import com.gec.system.util.AlipayConfig;
import com.gec.system.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO 沙箱控制器
 *
 * @author czp
 */
@RestController
@RequestMapping("/system/alipay")
public class AlipayController {
    /**
     * 阿里网关地址
     */
    private static final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 数据格式
     */
    private static final String FORMAT = "JSON";

    /**
     * 编码格式
     */
    private static final String CHARSET = "UTF-8";

    /**
     * 签名方式
     */
    private static final String SIGN_TYPE = "RSA2";

    @Autowired
    private AlipayConfig alipayConfig;

    /**
     * 请求支付包
     * 买家账号：ggldhv8859@sandbox.com
     * 支付密码：111111
     *
     * @param aliPay   与阿里交互的对象
     * @param response 响应
     * @throws IOException io流异常
     */
    @GetMapping("/pay")
    public void pay(AliPayBean aliPay, HttpServletResponse response) throws IOException {
        System.out.println("调用啦啊啊啊啊啊");
        //1.创建client，通过阿里SDK提供的client，负责调用支付宝的API
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, alipayConfig.getAppId(),
                alipayConfig.getAppPrivateKey(), FORMAT, CHARSET, alipayConfig.getAlipayPublicKey(),
                SIGN_TYPE);
        //2.创建request，并设置request参数
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(alipayConfig.getNotifyUrl());
        //设置支付完成后的页面跳转的路径
        request.setReturnUrl(alipayConfig.getReturnUrl());
        JSONObject bizContent = new JSONObject();
        System.out.println("out_trade_no"+aliPay.getOut_trade_no());
        System.out.println("total_amount"+aliPay.getTotal_amount()+1);
        System.out.println("subject"+ aliPay.getSubject());
        //支付名称
        bizContent.put("subject", aliPay.getSubject());
        //我们自己生成的订单编号
        bizContent.put("out_trade_no", aliPay.getOut_trade_no());
        //订单总金额
        bizContent.put("total_amount", aliPay.getTotal_amount());
        //支付名称
        bizContent.put("subject", aliPay.getSubject());
        //固定配置
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        request.setBizContent(bizContent.toString());
        //执行请求，拿到响应的结果，返回给浏览器
        String form = "";
        try {
            //调用SDK生成表单
            form = alipayClient.pageExecute(request).getBody();
            System.out.println("form "+form);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //设置响应结果，将返回的内容写出到浏览器
        response.setContentType("text/html;charset=" + CHARSET);
        //直接将完整的表单html输出到页面
        response.getWriter().write(form);
        response.getWriter().flush();
        response.getWriter().close();
    }

    /**
     * 支付宝回调【post】
     * 支付宝异步回调【必须是POST】
     * @return
     */
    @PostMapping("/notify")
    public Result<Object> payNotify(HttpServletRequest request) throws AlipayApiException {
        System.out.println("调用啦啊啊啊啊啊2222222");
        if(request.getParameter("trade_status").equals("TRADE_SUCCESS")){
            System.out.println("-------------支付宝异步回调----");
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            System.out.println("-----params-----------");
            for(String name : requestParams.keySet()){
                params.put(name, request.getParameter(name));
                System.out.println(name +"     " + request.getParameter(name));
            }
            String outTradeNo = params.get("out_trade_no");
            //支付时间
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            System.out.println("outTradeNo"+outTradeNo);
            System.out.println("gmtPayment"+gmtPayment);
            System.out.println("alipayTradeNo"+alipayTradeNo);
            //拿到签名
            String sign = params.get("sign");
            String content = AlipaySignature.getSignCheckContentV1(params);
            //验证签名
            boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign,
                    alipayConfig.getAlipayPublicKey(), "UTF-8");
            //支付宝验签
            if(checkSignature){
                //验签通过
                System.out.println("交易名称: " + params.get("subject"));
                System.out.println("交易状态: " + params.get("trade_status"));
                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
                System.out.println("商户订单号: " + params.get("out_trade_no"));
                System.out.println("交易金额: " + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
                System.out.println("买家付款时间: " + params.get("gmt_payment"));
                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
                // 查询订单
                //TODO 支付成功，操作数据库，创建对应订单，修改对应商品数据
            }

        }
        return Result.ok();
    }
}